package com.igoal.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;
import org.hibernate.annotations.Type;

@Entity
@Table(name="user_activity")
public class UserActivity extends BaseObject implements Audit, ItemOwner {
	private static final long serialVersionUID = 7631578183545451533L;

	private Long id;
	private User componentOwner;
	private User actionOwner;
	private ActionTypeEnum actionType;
	private Long componentId;
	private ComponentEnum component;
	private boolean read;
	private Date createDate;
	private Date updateDate;
	private DataStatus status;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@SearchableId
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	@ManyToOne
	@JoinColumn(name="component_owner_id")
	public User getComponentOwner() {
		return componentOwner;
	}

	public void setComponentOwner(User user) {
		this.componentOwner = user;
	}

	@ManyToOne
	@JoinColumn(name="action_owner_id")
	public User getActionOwner() {
		return actionOwner;
	}

	public void setActionOwner(User actionOwner) {
		this.actionOwner = actionOwner;
	}

	@Enumerated(EnumType.STRING)
	@Column(nullable = false, name = "action_type")
	@SearchableProperty
	public ActionTypeEnum getActionType() {
		return actionType;
	}

	public void setActionType(ActionTypeEnum actionType) {
		this.actionType = actionType;
	}

	@Column(nullable = false, name = "component_id")
	public Long getComponentId() {
		return componentId;
	}

	public void setComponentId(Long componentId) {
		this.componentId = componentId;
	}

	@Enumerated(EnumType.STRING)
	@Column(name = "component", nullable = false)
	public ComponentEnum getComponent() {
		return component;
	}

	public void setComponent(ComponentEnum component) {
		this.component = component;
	}

	@Column(name = "is_read", nullable = false)
	@Type(type = "yes_no")
	public boolean isRead() {
		return read;
	}

	public void setRead(boolean read) {
		this.read = read;
	}

	@Enumerated(EnumType.STRING)
	@Column(nullable = false, name = "status")
	@SearchableProperty
	public DataStatus getStatus() {
		return status;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable = false, name = "mdate")
	public Date getUpdateDate() {
		return updateDate;
	}

	public void setCreateDate(Date date) {
		this.createDate = date;
	}

	public void setStatus(DataStatus dataStatus) {
		this.status = dataStatus;
	}

	public void setUpdateDate(Date date) {
		this.updateDate = date;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable = false, name = "cdate")
	public Date getCreateDate() {
		return createDate;
	}
	
	@Transient
	public User getOwner() {
		return actionOwner;
	}
	
	@Override
	public boolean equals(Object o) {
		if(o==null)
			return false;
		
		UserActivity that = (UserActivity) o;
		return this.id.equals(that.id);
	}

	@Override
	public int hashCode() {
		return this.id.hashCode();
	}

	@Override
	public String toString() {
		// if(componentOwner != null && StringUtils.isBlank(actionOwner)){
		// return actionOwner + actionType + " on " + component +
		// "[id="+componentId+"] of user " + componentOwner.getUsername();
		// }
		return "UserNotificationLog";
	}

}
